<?php
/**
 * CreateTime: 2022/4/30 15:38
 */

namespace App\ServiceChecker\Dictionary;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Models\Dictionary\DictionaryModel;
use App\ServiceChecker\BasicServiceChecker;
use Illuminate\Http\Exceptions\HttpResponseException;

class DictionaryLegitimacy extends BasicServiceChecker
{
    private DictionaryModel $dictionaryModel;

    public function __construct()
    {
        parent::__construct();
        $this->dictionaryModel = new DictionaryModel();
    }

    /**
     * 校验字典是否已经存在
     * @param $dictionaryKey mixed 字典key
     */
    public function verifyDictionaryIsExist(mixed $dictionaryKey)
    {
        $isExist = $this->dictionaryModel->isExistByCondition([
            'dictionary_key' => $dictionaryKey,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                '',
                '当前字典关键字已存在',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    public function verifyDictionaryParentIsExist(mixed $parent_id)
    {
        if(!$parent_id){
            return true;
        }

        $isExist = $this->dictionaryModel->isExistByCondition([
            'dictionary_id' => $parent_id,
            'status' => BaseConsts::STATUS_NORMAL
        ]);

        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                '',
                '父级不存在',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }
}